package com.example.communitypro.service.impl;

import com.example.communitypro.mapper.UserBrowseHistoryMapper;
import com.example.communitypro.pojo.UserBrowseHistory;
import com.example.communitypro.service.UserBrowseHistoryService;
import com.example.communitypro.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserBrowseHistoryServiceImpl implements UserBrowseHistoryService {
    @Autowired(required=false)
    private UserBrowseHistoryMapper userBrowseHistoryMapper;


    @Override
    public Result addUserBrowseHistory(UserBrowseHistory record){
        try {
            if(userBrowseHistoryMapper.insertSelective(record)>0)
            {
                return Result.ok().message("新增用户浏览记录成功");
            }
            else{
                return Result.error().message("新增用户浏览记录失败");
            }
        }catch (Exception e){
            return Result.error().message("新增用户浏览记录失败");
        }
    }


    @Override
    public Result dropUserBrowseHistory(int browseRecordNo){
        try {
            if(userBrowseHistoryMapper.deleteByPrimaryKey(browseRecordNo)>0)
            {
                return Result.ok().message("删除用户浏览记录成功");
            }
            else{
                return Result.error().message("删除用户浏览记录失败");
            }
        }catch (Exception e){
            return Result.error().message("删除用户浏览记录失败");
        }
    }

    @Override
    public Result modifyUserBrowseHistory(UserBrowseHistory record){
        try {
            if(userBrowseHistoryMapper.updateByPrimaryKeySelective(record)>0)
            {
                return Result.ok().message("修改成功");
            }
            else{
                return Result.error().message("修改失败：未找到对应记录");
            }
        }catch (DuplicateKeyException e){
            System.err.println(e);
            return Result.error().message("修改失败：已经存在对应记录");
        }
        catch (Exception e){
            System.err.println(e);
            return Result.error().message("修改失败："+e.toString());
        }
    }

    @Override
    public Result queryUserBrowseHistory(int browseRecordNo){
        try {
            UserBrowseHistory userBrowseHistory=userBrowseHistoryMapper.selectByPrimaryKey(browseRecordNo);
            if(userBrowseHistory!=null)
            {
                return Result.ok().message("查找用户浏览记录成功").data("userBrowseHistory",userBrowseHistory);
            }
            else{
                return Result.error().message("查找用户浏览记录失败");
            }
        }catch (Exception e){
            return Result.error().message("查找用户浏览记录失败");
        }
    }

    @Override
    public Result queryByUser(int userNo) {
        try {
            List<UserBrowseHistory> userBrowseHistorys=userBrowseHistoryMapper.selectByUser(userNo);
            if(userBrowseHistorys!=null)
            {
                return Result.ok().message("查找用户全部浏览记录成功").data("userBrowseHistorys",userBrowseHistorys);
            }
            else{
                return Result.error().message("查找用户全部浏览记录成功");
            }
        }catch (Exception e){
            return Result.error().message("查找用户全部浏览记录成功");
        }
    }

    @Override
    public Result dropByUser(int userNo) {
        try {
            if(userBrowseHistoryMapper.deleteByUserNo(userNo)>0)
            {
                return Result.ok().message("删除用户全部浏览记录成功");
            }
            else{
                return Result.error().message("删除用户全部浏览记录失败");
            }
        }catch (Exception e){
            return Result.error().message("删除用户全部浏览记录失败");
        }
    }
}
